package edu.chen.service.impl;

import edu.chen.dao.UserDao;
import edu.chen.entity.User;
import edu.chen.exception.login.LoginException;
import edu.chen.exception.login.NoUserException;
import edu.chen.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserDao userDao;

    @Transactional
    public User verifyLogin(String username, String password) {
        User user = userDao.queryByUsername(username);  //尝试以用username查询User
        if(user == null)   //账号不存在时
            throw new NoUserException("用户不存在");
        if(user.getPassword().equals(password)){
            return user;
//            return new BasicExecution<User>(LoginStateEnum.SUCCESS,user);
        }else{
            throw new LoginException("密码错误");
        }
    }


    @Transactional
    public User getUserInfo(String username) {
        return userDao.queryByUsername(username);
    }

    @Transactional
    public void updateUserInfo(User user){
        userDao.update(user);
    }
}
